package com.tentative.competition.web.controller.schedule.service;

import cn.hutool.core.collection.CollUtil;
import com.tentative.competition.common.annotation.Anonymous;
import com.tentative.competition.common.core.controller.BaseController;
import com.tentative.competition.common.core.domain.AjaxResult;
import com.tentative.competition.common.core.result.ApiResult;
import com.tentative.competition.common.core.result.PageResult;
import com.tentative.competition.common.model.param.SubmitWorksApplyParam;
import com.tentative.competition.common.model.schedule.param.ScheduleWorkSearchParam;
import com.tentative.competition.common.model.schedule.param.ScheduleWorksParam;
import com.tentative.competition.common.model.schedule.resp.ScheduleWorksInfoResp;
import com.tentative.competition.schedule.domain.ScheduleWorks;
import com.tentative.competition.schedule.service.ScheduleWorksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther: Smile Huang
 * @date: 2023/6/22 22:05
 * @desc: 作品控制器
 * @version: 2.0
 */
@Api(tags = "service-作品业务接口")
@RestController
@RequestMapping("/service/works")
public class MgWorksController extends BaseController {

    @Autowired
    private ScheduleWorksService worksService;

    /**
     * 提交或修改作品
     * @param param
     * @return
     */
    @ApiOperation("提交或修改作品")
    @PostMapping("/submit-works")
    public AjaxResult submitWorks(@RequestBody ScheduleWorksParam param) {
        worksService.submitMgWorks(param);
        return AjaxResult.success();
    }

    /**
     * 提交或修改作品
     * @param param
     * @return
     */
    @ApiOperation("更新作品是否首页展示")
    @PostMapping("/updateHomeShowById")
    public AjaxResult updateHomeShowById(@RequestBody ScheduleWorksParam param) {
        worksService.updateHomeShowById(param.getId());
        return AjaxResult.success();
    }

    /**
     * 提交或修改作品
     * @param param
     * @return
     */
    @ApiOperation("更新作品是否往届展示")
    @PostMapping("/updateBackShowById")
    public AjaxResult updateBackShowById(@RequestBody ScheduleWorksParam param) {
        worksService.updateBackShowById(param.getId());
        return AjaxResult.success();
    }

    /**
     * 作品列表
     * @return
     */
    @ApiOperation("获取作品列表")
    @PostMapping("/list")
    public ApiResult list(@RequestBody ScheduleWorkSearchParam param) {
        PageResult<ScheduleWorksInfoResp> pageResult = worksService.selectWorksList(param);
        return ApiResult.ofPage(pageResult.getContent(),pageResult.getTotal());
    }

    /**
     * 作品明细
     * @param id
     * @return
     */
    @ApiOperation("获取作品明细")
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(worksService.getInfo(id));
    }

    @ApiOperation("删除作品")
    @PostMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        if (CollUtil.isEmpty(ids)){
            throw new RuntimeException("没有需要删除的数据！");
        }
        return toAjax(worksService.lambdaUpdate()
                .in(ScheduleWorks::getId,ids)
                .set(ScheduleWorks::getDelFlag,1)
                .update());
    }

    @ApiOperation("获取当前登录人下得所有作品信息-(草稿状态)")
    @GetMapping(value = "/worksByUser")
    public AjaxResult getWorksKeyVul() {
        return success(worksService.getWorksKeyVul());
    }

    @ApiOperation("提交或修改报名信息")
    @PostMapping("/submit-apply")
    public AjaxResult submitApply(@RequestBody SubmitWorksApplyParam param) {
        return AjaxResult.success(worksService.submitApply(param));
    }

    @ApiOperation("获取当前用户下的拟参赛作品信息")
    @GetMapping(value = "/imitateWorksInfo")
    public AjaxResult getImitateWorksInfo() {
        return AjaxResult.success(worksService.getImitateWorksInfo());
    }

    @ApiOperation("根据作品ID获取成员信息")
    @GetMapping(value = "/memberInfo/{worksId}")
    public AjaxResult getMemberInfoByWorksId(@PathVariable("worksId") Long worksId) {
        return success(worksService.getMemberInfoByWorksId(worksId));
    }

    @ApiOperation("作品点赞")
    @Anonymous
    @GetMapping(value = "/upvote/{worksId}")
    public AjaxResult upvote(@PathVariable("worksId") Long worksId) {
        return success(worksService.upvote(worksId));
    }

    @ApiOperation("获取奖项下拉数据")
    @Anonymous
    @GetMapping(value = "/awards/pull-down/{scheduleId}")
    public AjaxResult awardsPullDown(@PathVariable("scheduleId") Long scheduleId) {
        return success(worksService.awardsPullDown(scheduleId));
    }
    /**
     * 更新注册信息
     * @return
     */
    @ApiOperation("获取作品根据用户ID")
    @GetMapping("/getWorksByUserId")
    public ApiResult getWorksByUserId(Long userId) {
        List<ScheduleWorks> list = worksService.getWorksByUserId(userId);
        return ApiResult.ok(list);
    }
    /**
     * 根据作品ID更新sort
     * @return
     */
    @ApiOperation("根据作品ID更新sort")
    @GetMapping("/updateSortByWorkId")
    public ApiResult updateSortByWorkId(Long worksId,Integer sort) {
        worksService.lambdaUpdate().eq(ScheduleWorks::getId,worksId).set(ScheduleWorks::getSort,sort).update();
        return ApiResult.ok();
    }

}
